package ideamaps.action;

import ideamaps.domain.Request;
import ideamaps.domain.User;
import ideamaps.manager.FriendManager;
import ideamaps.manager.UserManager;

import com.taillive.core.web.StrutsAction;

public class LoginAction extends StrutsAction {

	private static final long serialVersionUID = 5451838843877578124L;
	private UserManager userManager;

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	private FriendManager friendManager;
	public void setFriendManager(FriendManager friendManager) {
		this.friendManager = friendManager;
	}
    
	
	private Request req;
	
	public Request getReq() {
		return req;
	}

	public void setReq(Request req) {
		this.req = req;
	}
	
	private String username;
	private String password;
	private long lat;
	private long lng;
	private String status;
	
	public long getLat() {
		return lat;
	}

	public void setLat(long lat) {
		this.lat = lat;
	}

	public long getLng() {
		return lng;
	}

	public void setLng(long lng) {
		this.lng = lng;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String login(){
       
		if(isGet()){
			return INPUT;
		}else{
			User user = userManager.getByUsername(username);
			if(user==null || !password.equals(user.getPassword())){
				addActionError("error password");
				return INPUT;
			}
			
			getSession().setAttribute("sessionUsername",user.getUsername());
			getSession().setAttribute("requestsCount", friendManager.countRequests(user.getUsername()));
			
		return SUCCESS;
	}
		
	}
	
	
	public void validateLogin(){
		
		if(isGet())
			return;
		
		if(username==null || username.trim().equals("") ){
			addActionError("Username is required");
		}
		if(password==null || password.trim().equals("")){
			addActionError("Password is required");
		}
	}


	
}
